package icu.qx.qc.controller;

import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.security.utils.SecurityUtils;
import icu.qx.common.utils.CodeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import icu.qx.qc.domain.QcDrawback;
import icu.qx.qc.service.IQcDrawbackService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 缺陷Controller
 *
 * @author ruoyi
 * @date 2024-06-11
 */
@RestController
@RequestMapping("/qc/drawback")
public class QcDrawbackController extends BaseController {
    @Autowired
    private IQcDrawbackService qcDrawbackService;
    @Autowired
    private CodeUtils codeUtils;

    /**
     * 查询缺陷列表
     */
    @GetMapping("/list")
    public TableDataInfo list(QcDrawback qcDrawback) {
        startPage();
        List<QcDrawback> list = qcDrawbackService.selectQcDrawbackList(qcDrawback);
        return getDataTable(list);
    }

    /**
     * 导出缺陷列表
     */
    @RequiresPermissions("qc:drawback:export")
    @Log(title = "缺陷", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, QcDrawback qcDrawback) {
        List<QcDrawback> list = qcDrawbackService.selectQcDrawbackList(qcDrawback);
        ExcelUtil<QcDrawback> util = new ExcelUtil<QcDrawback>(QcDrawback.class);
        util.exportExcel(response, list, "缺陷数据");
    }

    /**
     * 获取缺陷详细信息
     */
    @RequiresPermissions("qc:drawback:query")
    @GetMapping(value = "/{drawbackCode}")
    public AjaxResult getInfo(@PathVariable("drawbackCode") String drawbackCode) {
        return success(qcDrawbackService.selectQcDrawbackByDrawbackCode(drawbackCode));
    }

    /**
     * 新增缺陷
     */
    @RequiresPermissions("qc:drawback:add")
    @Log(title = "缺陷", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody QcDrawback qcDrawback) {
        qcDrawback.setDrawbackCode(codeUtils.generateCode("qc_drawback"));
        return toAjax(qcDrawbackService.insertQcDrawback(qcDrawback));
    }

    /**
     * 修改缺陷
     */
    @RequiresPermissions("qc:drawback:edit")
    @Log(title = "缺陷", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody QcDrawback qcDrawback) {
        return toAjax(qcDrawbackService.updateQcDrawback(qcDrawback));
    }

    /**
     * 删除缺陷
     */
    @RequiresPermissions("qc:drawback:remove")
    @Log(title = "缺陷", businessType = BusinessType.DELETE)
    @DeleteMapping("/{drawbackCodes}")
    public AjaxResult remove(@PathVariable String[] drawbackCodes) {
        return toAjax(qcDrawbackService.deleteQcDrawbackByDrawbackCodes(drawbackCodes));
    }
}
